﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;

namespace enhanced_command_interface
{
    public class KinectInterface
    {
        //TODO: Expand class to allow caller to select which sensor to use?

        private KinectSensor sensor;
        private WriteableBitmap colorBitmap;
        public byte[] colorPixels;

        public KinectInterface()
        {
            Refresh();
        }

        public void Refresh()
        {
            foreach (var potentialSensor in KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == KinectStatus.Connected)
                {
                    this.sensor = potentialSensor;
                    break;
                }
            }

            if (null != this.sensor)
            {
                // Turn on the color stream to receive color frames
                this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);

                // Allocate space to put the pixels we'll receive
                this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];

                // This is the bitmap we'll display on-screen
                this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);

                // Add an event handler to be called whenever there is new color frame data
                this.sensor.ColorFrameReady += this.displayImage;
                //             this.sensor.DepthFrameReady += this.displayImage;

            }

            if (null == this.sensor)
            {
                //MessageBox.Show("aaah");
            }
        }

        public bool StartKinect()
        {
            try
            {
                this.sensor.Start();
                return true;
            }
            catch (IOException)
            {
                this.sensor = null;
                return false;
            }
        }

        public bool StopKinect()
        {
            try
            {
                this.sensor.Stop();
                return true;
            }
            catch (IOException)
            {
                this.sensor = null;
                return false;
            }
        }

        public WriteableBitmap getBitMap()
        {
            //Up to the caller to set their imagebox source to this in order to get the stream.
            return this.colorBitmap;
        }


        private void displayImage(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * colorFrame.BytesPerPixel,
                        0);
                }

            }
        }

    }
}
